/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.jome.ctrl.amto;

/**
 *
 * @author Mr.Sam
 */
import fr.ove.openmath.jome.ctrl.linear.events.LinearParserEvent;
import fr.ove.openmath.jome.ctrl.amto.MapsToNAry;

/**
* The abstract math tree object of a n-ary operator which behaves like a list.<BR>
* This category is materialised by (of course) lists, interval, n-ary functions, ...<BR>
* When the instance is flushed, the sequence of events sent is the following :
* <UL>
* <LI> an event for the opening </LI>
* <LI> the flush of each child (so, as many events as necessary for the flush).<BR>
* Between each flush of a child, an event which represents a separator is also sent.
* (for n children, n-1 separator are sent, so n-1 events)</LI>
* <LI> an event for the closing </LI>
* </UL>
* Lets consider as example the list with 1 and 2 as elements.<BR>
* Lets also this consider list having a the syntax : {1,2}.<BR>
* The flush of this list produces the events for :
* <UL>
* <LI>an event for { (what is called the opening)</LI>
* <LI>an event for 1</LI>
* <LI>an event for , (what is called the separator)</LI>
* <LI>an event for 2</LI>
* <LI>an event for } (what is called the closing)</LI>
* </UL>
*
* @author © 1999 DIRAT Laurent
* @version 1.0  18/11/1999
*/
public abstract class MapsToList extends MapsToNAry {
    /**
    * The opening.
    */
    protected int opening;
    
    /**
    * The closing.
    */
    protected int closing;
    
    /**
    * The constructor.
    * @param identifier the identifier of the instance.
    * @param token the identifier of the instance by the mean of the event sent.
    */
    public MapsToList(String identifier, int opening, int closing) {
        super(identifier, LinearParserEvent.SEPARATOR);
        // We consider the token as a separator.
        // That way, when the instance is flushed, we've got something like : c0, c1, c2, ...., cn
        // where the cn reprent the children of the instance.
        // The inherited classe
        this.opening = opening;
        this.closing = closing;
    }

    /**
    * "Flushes" the object as a sequence of events for the building of the model
    */
    public void flush() {
        LinearParserEvent linearParserEvent = new LinearParserEvent(getEventSource());
        
        // The opening
        linearParserEvent.setToken(opening, param);
        fireLinearParserEvent(linearParserEvent);
        
        String oldParam = param; // To respect coherence, param is set to null
                                 // (the token LinearParserEvent.SEPARATOR, as no parameter)
        param = null;
        // call to super.flush() to have the operands
        super.flush();
        
        param = oldParam; // To respect coherence, we set param to the value that's its.
        
        // The closing
        linearParserEvent.setToken(closing, null);
        fireLinearParserEvent(linearParserEvent);
    }
}